<?php

class image_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function image_style_rename($name, $w, $h)
    {
        //$name = 'Chrysanthemum.html.jpg';
        $ext = strrchr($name, '.');
        $i = strrpos($name, '.');
        $name = substr($name, 0, $i);
        $newname = $name . '_'.$w.'_'.$h . $ext;
        return $newname;
    }
    function image_style_url($uri, $w = 50, $h = 50)
    {
        //$root_dir = 'C:/xampp/htdocs/hieuhoc/';
        $root_dir = ROOT_DIR_PATH.'/';
        
        $name = basename($uri);
        $newName = $this->image_style_rename($name, $w, $h);
        $newName = str_replace($name, 'thumb/'.$newName, $uri);
                
        if (!file_exists($root_dir . $newName)) {
            $config['image_library'] = 'gd2';
            $config['source_image'] = $root_dir. $uri;
            $config['new_image'] = $root_dir . $newName;
            //$config['create_thumb'] = true;
            $config['maintain_ratio'] = true;
            $config['width'] = $w;
            $config['height'] = $h;
            $this->load->library('image_lib', $config);
            
            $this->image_lib->resize();    
        }    
        
        
        return root_url().$newName;
    }
}